package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Collect;
import com.example.domain.Product;
import com.example.domain.Product_category;
import com.example.result.JsonMap;
import com.example.result.JsonResult;
import com.example.service.ProductService;
import com.example.service.Product_categoryService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController extends BaseController{
    @Autowired
    private ProductService productService;
    @Autowired
    private Product_categoryService product_categoryService;
    //热销排行
    @PostMapping("/getBestList/{currentPage}/{pageSize}")
    public JsonResult getBestList(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        IPage<Product> iPage=new Page<>(currentPage,pageSize);
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.eq("status",1);
        wrapper.orderBy(true,true,"priority","title");  //优先级和标题
        IPage<Product> page = productService.page(iPage, wrapper);
        if(page==null){
            throw new RuntimeException("数据不存在");
        }

        return new JsonResult(true,"",page);
    }
    //详情页数据获取
    @GetMapping("/details")
    public JsonResult details(Long id){
        Product product = productService.getById(id);
        if(product==null){
            throw new RuntimeException("商品信息不存在");
        }
        return new JsonResult(true,"",product);
    }
    //推荐栏目
    @PostMapping("/getRecommendList/{currentPage}/{pageSize}")
    public JsonResult getRecommendList(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        IPage<Product> iPage=new Page<>(currentPage,pageSize);
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.eq("status",1);
        wrapper.orderBy(true,true,"modified_time","title");  //优先级和标题
        IPage<Product> page = productService.page(iPage, wrapper);
        return new JsonResult(true,"",page);
    }
    //获取分类
    @GetMapping("/getListProductType")
    public JsonResult getListProductType(){
        QueryWrapper<Product_category> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<Product_category> list = product_categoryService.list(wrapper);
        if(list==null){
            throw new RuntimeException("哔哔");
        }
        return new JsonResult(true,"",list);
    }
    //获取搜索详情页信息
    @PostMapping("/getPageList/{currentPage}/{pageSize}")
    public JsonMap getPageList(@PathVariable Integer currentPage, @PathVariable Integer pageSize,String context){
        System.out.println("获取到的参数"+context);
        IPage<Product> iPage=new Page<>(currentPage,pageSize);
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        if(context==null||"".equals(context)){
            wrapper.like("title",context);
            wrapper.or();
            wrapper.like("item_type",context);
        }
        wrapper.orderByDesc("modified_time","title");
        IPage<Product> page = productService.page(iPage, wrapper);
        if(page==null){
            throw new RuntimeException("数据有误,请刷新");
        }
        List<List<Product>> resultList=new ArrayList<>();


        for(int index=0;index<page.getRecords().size();index=index+4){
            List<Product> temp=new ArrayList<>();
            for(int i=index;i<page.getRecords().size();i++){
                if(page.getRecords().get(i)!=null&&i<index+4){
                    temp.add(page.getRecords().get(i));
                }
            }
            resultList.add(temp);
        }
        page.setRecords(null);
        return new JsonMap(page,resultList);
    }
}
